package ch.fhnw.qualiscan.application.modals;

import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.db.pojos.FinalEvaluation;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.libraries.NotificationLibrary;
import eu.livotov.tpt.i18n.TM;

public class EditFinalEvaluation extends Modal{
	private static final long serialVersionUID = 1L;

	private SurveyUserRole sur;
	
	private RichTextArea editor;
	private TextField title;
	
	private SurveyModel surveyModel;
	
	private VerticalLayout verticalLayout;
	private FinalEvaluation finalEvaluation;
	
	private String titleString = "";
	private String textString = "";
	
	public EditFinalEvaluation(SurveyUserRole sur) {
		this.sur = sur;
		this.setCaption(TM.get("finalreport.title"));
		
		surveyModel = new SurveyModel();
		
		finalEvaluation = surveyModel.getFinalEvaluation(sur);
		if (finalEvaluation != null) {
			titleString = finalEvaluation.getTitle();
			textString = finalEvaluation.getFinalevaluation();
		}
		buildLayout();
	}
	
	public void buildLayout() {
		
		verticalLayout = new VerticalLayout();
		Label desc = textHelper.p(TM.get("finalreport.desc"));
		verticalLayout.addComponent(desc);
		
		HorizontalLayout titleWrap = new HorizontalLayout();
		Label titleLabel = textHelper.span(TM.get("finalreport.form.title"));
		titleLabel.setWidth("160px");	
		
		title = new TextField();
		title.setValue(titleString);
		title.setColumns(25);
		titleWrap.addComponent(titleLabel);
		titleWrap.addComponent(title);
		
		HorizontalLayout textWrap = new HorizontalLayout();
		Label textLabel = textHelper.span(TM.get("finalreport.form.text"));
		textLabel.setWidth("160px");
		
		editor = new RichTextArea();
		editor.setValue(textString);
		editor.setWidth("100%");
		
		textWrap.addComponent(textLabel);
		textWrap.addComponent(editor);
		
		Button saveButton = new Button(TM.get("action.save"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				surveyModel.saveFinalEvaluation(title.getValue().toString(), editor.getValue().toString(), sur);
				
				NotificationLibrary.showSuccess(TM.get("action.saved"), TM.get("finalreport.form.saved"));
				EditFinalEvaluation efe = (EditFinalEvaluation) event.getButton().getData();
				efe.close();
			}
		});
		saveButton.setIcon(Config.ICON_SAVE);
		saveButton.setData(this);
		
		Button cancelButton = new Button(TM.get("action.cancel"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				EditFinalEvaluation efe = (EditFinalEvaluation) event.getButton().getData();
				efe.close();
			}
		});
		cancelButton.setIcon(Config.ICON_CANCEL);
		cancelButton.setData(this);
		
		HorizontalLayout buttonBar = new HorizontalLayout();
		buttonBar.setSpacing(true);
		buttonBar.addComponent(saveButton);
		buttonBar.addComponent(cancelButton);
		
		verticalLayout.addComponent(titleWrap);
		verticalLayout.addComponent(textWrap);
		verticalLayout.addComponent(buttonBar);
		
		verticalLayout.setSpacing(true);
		addComponent(verticalLayout);
		
	}
}
